package com.siyoumi.app.modules.sh.admin;

import com.siyoumi.app.entity.ShFund;
import com.siyoumi.app.modules.sh.entity.EnumShCmdRun;
import com.siyoumi.app.modules.sh.service.SvcShFund;
import com.siyoumi.app.modules.sh.vo.ShApiFundInfo;
import com.siyoumi.app.modules.sh.vo.ShFundData;
import com.siyoumi.app.modules.sh.vo.VaShFund;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.*;
import com.siyoumi.validator.XValidator;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/xadmin/sh/sh__fund__edit")
public class sh__fund__edit
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("基金-编辑");

        Map<String, Object> data = new HashMap<>();
        data.put("fund_enable", 0);
        data.put("fund_order", 0);
        ShFundData fundData = new ShFundData();
        if (isAdminEdit()) {
            ShFund entity = SvcShFund.getApp().getEntity(getID());

            HashMap<String, Object> dataAppend = new LinkedHashMap<>();
            dataAppend.put("id", entity.getKey());
            //
            fundData = entity.fundData();
            //
            //合并
            data = entity.toMap();
            data.putAll(dataAppend);
        }
        getR().setData("data", data);
        setPageInfo("fund_data", fundData);

        //运行状态
        LinkedHashMap<String, String> run = IEnum.toMap(EnumShCmdRun.class);
        setPageInfo("run", run);


        return getR();
    }


    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/save")
    public XReturn save(@Validated() VaShFund vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);
        //
        {
            JoinWrapperPlus<ShFund> query = SvcShFund.getBean().listQuery();
            query.eq("fund_code", vo.getFund_code());
            if (isAdminEdit()) {
                query.ne("fund_id", getID());
            }
            ShFund entity = SvcShFund.getApp().first(query);
            if (entity != null) {
                result.addError(XValidator.getErr("fund_code", "基金编号已存在"));
            }
        }
        if (XStr.isNullOrEmpty(vo.getFund_data())) {
            result.addError(XValidator.getErr("buy_net", "基金参数缺少"));
        }
        XValidator.getResult(result);
        //
        List<String> ignoreField = new ArrayList<>();
        if (isAdminEdit()) {

        } else {
        }

        ShApiFundInfo apiFundInfo = SvcShFund.getBean().getApiFundInfo(vo.getFund_code());
        //赋值名称
        vo.setFund_name(apiFundInfo.getName());
        //赋值编号
        {
            ShFundData shFundData = XJson.parseObject(vo.getFund_data(), ShFundData.class);
            shFundData.setFund_code(vo.getFund_code());
            vo.setFund_data(XJson.toJSONString(shFundData));
        }



        InputData inputData = InputData.fromRequest();

        return SvcShFund.getApp().saveEntity(inputData, vo, true, ignoreField);
    }

    /**
     * 基金信息
     * <p>
     * name：名称
     * fundcode：基金代码
     * gztime：更新时间
     * gsz：最新净值
     * gszzl：升降百分比
     * {'fundcode': '008929', 'name': '宏利消费红利指数C', 'jzrq': '2025-04-29', 'dwjz': '1.5122', 'gsz': '1.4953', 'gszzl': '-1.12', 'gztime': '2025-04-30 15:00'}
     */
    @GetMapping("/get_fund_net")
    public XReturn getFundNet() {
        String fundCode = input("fund_code");
        XValidator.isNullOrEmpty(fundCode, "miss fund_code");

        ShApiFundInfo fundInfo = SvcShFund.getBean().getApiFundInfo(fundCode);
        XReturn r = XReturn.getR(0);
        r.setData("fund_info", fundInfo);
        return r;
    }
}
